import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:mahjong/const/MahjongConst.dart';
import 'package:mahjong/entity/mahjong/MahjongInfoEntity.dart';
import 'package:mahjong/entity/user/MahjongUserInfo.dart';
import 'package:mahjong/page/GameIndex.dart';
import 'package:mahjong/util/CustomRoute.dart';
import 'package:mahjong/util/DictionaryConst.dart';
import 'package:mahjong/util/ImageUtil.dart';
import 'package:mahjong/const/MahjongSystemConst.dart';
import 'package:mahjong/util/BottomClipper.dart';
import 'package:provider/provider.dart';

class Settlement extends StatefulWidget {
  @override
  _SettlementState createState() => _SettlementState();
}

class _SettlementState extends State<Settlement> {
  GlobalKey globalKey = GlobalKey();
  double bodyWidth = ScreenUtil().screenHeight;
  double bodyHeight = ScreenUtil().screenHeight * 7 / 8;
  @override
  Widget build(BuildContext context) {
    return Consumer(builder: (BuildContext buildContext,
        MahjongInfoEntity roomProvider, Widget child) {
      if (roomProvider.allUser == null) {
        return Container();
      }
      if (roomProvider.state != RoomState.OVER) {
        return Container();
      }
      return Align(
          alignment: Alignment.center,
          child: RepaintBoundary(
            key: globalKey,
            child: Container(
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(5.5),
                ),
                width: bodyWidth,
                height: bodyHeight,
                child: Stack(
                  children: [
                    Positioned(
                      top: 20.h,
                      left: 0,
                      bottom: 0,
                      child: Container(
                        padding: EdgeInsets.symmetric(horizontal: 10.w),
                        height: bodyHeight,
                        width: bodyWidth,
                        decoration: BoxDecoration(
                            borderRadius: BorderRadius.circular(5.5),
                            boxShadow: [
                              BoxShadow(
                                color: Colors.black,
                                offset: Offset(10.00, 10.00),
                                blurRadius: 10.0,
                              ),
                            ]),
                      ),
                    ),
                    Positioned(
                        top: 20.h,
                        left: 0,
                        bottom: 0,
                        child: ClipPath(
                            //路径裁切组件
                            clipper: BottomClipper(), //路径
                            child: Container(
                              padding: EdgeInsets.symmetric(horizontal: 10.w),
                              height: bodyHeight - 20.h,
                              width: bodyWidth,
                              decoration: BoxDecoration(
                                  borderRadius: BorderRadius.circular(5.5),
                                  color: Colors.white,
                                  boxShadow: [
                                    BoxShadow(
                                      color: Colors.red,
                                      offset: Offset(100, 100),
                                      blurRadius: 100.0,
                                    ),
                                  ]),
                              child: SingleChildScrollView(
                                  child: Column(children: [
                                SizedBox(
                                  height: 20.h,
                                ),
                                Container(
                                  color: Colors.blue[100].withOpacity(0.5),
                                  child: Row(
                                    mainAxisAlignment:
                                        MainAxisAlignment.spaceEvenly,
                                    children: [
                                      Text("万州麻将",
                                          style: TextStyle(
                                              color: Colors.grey.shade600,
                                              fontSize: MahjongSystemConst
                                                  .bodyTextFontDefalutSize)),
                                      Text(
                                        "房间号:" + roomProvider.code,
                                        style: TextStyle(
                                            color: Colors.grey.shade600,
                                            fontSize: MahjongSystemConst
                                                .bodyTextFontDefalutSize),
                                      ),
                                      Text(
                                        "结束时间:" + roomProvider.gameEndDate,
                                        style: TextStyle(
                                            color: Colors.grey.shade600,
                                            fontSize: MahjongSystemConst
                                                .bodyTextFontDefalutSize),
                                      )
                                    ],
                                  ),
                                ),
                                SizedBox(
                                  height: ScreenUtil().setHeight(10),
                                ),
                                Column(
                                    children: roomProvider.allUser
                                        .map((userInfo) => buildUserBillItem(
                                            userInfo: userInfo,
                                            backgroundColor: Colors.orange[100]
                                                .withOpacity(0.5)))
                                        .toList()),
                                Row(
                                  mainAxisAlignment:
                                      MainAxisAlignment.spaceEvenly,
                                  crossAxisAlignment: CrossAxisAlignment.center,
                                  children: [
                                    Container(
                                        width: bodyWidth / 4,
                                        height: bodyHeight / 10,
                                        child: OutlinedButton(
                                          onPressed: () {
                                            ImageUtil.saveWidgetImage(
                                                globalKey);
                                          },
                                          child: Text("存至相册"),
                                        )),
                                    Container(
                                        width: bodyWidth / 4,
                                        height: 30.h,
                                        child: OutlinedButton(
                                          onPressed: () {
                                            Navigator.of(context)
                                                .pushAndRemoveUntil(
                                                    new CustomRoute(GameIndex(),
                                                        AnimationEnum.zoom),
                                                    (route) => route == null);
                                          },
                                          child: Text("游戏结束"),
                                        )),
                                  ],
                                ),
                                SizedBox(
                                  height: ScreenUtil().setHeight(20),
                                ),
                                Container(
                                    height: bodyHeight / 5,
                                    child: Row(
                                      mainAxisAlignment:
                                          MainAxisAlignment.spaceEvenly,
                                      children: [
                                        Center(
                                          child: Text(
                                            "禁止赌博\n此结果仅供娱乐参考\n(本平台不提供任何充值通道)",
                                            textAlign: TextAlign.center,
                                            style: TextStyle(
                                                color: Colors.black,
                                                fontSize: MahjongSystemConst
                                                    .bodyTextFontMiniSize),
                                          ),
                                        ),
                                        Text(
                                          "--欢迎下载--\n--逍遥棋牌--",
                                          textAlign: TextAlign.center,
                                          style: TextStyle(
                                              color: Colors.black,
                                              fontSize: MahjongSystemConst
                                                  .bodyTextFontMiniSize),
                                        ),
                                        Container(
                                            padding: EdgeInsets.symmetric(
                                                vertical:
                                                    ScreenUtil().setHeight(10)),
                                            child: Image.asset(
                                              "images/logo/dow_qr.png",
                                              fit: BoxFit.cover,
                                            )),
                                      ],
                                    )),
                              ])),
                            ))),
                    Align(
                        alignment: Alignment(0, -1),
                        child: Container(
                            width: 100.w,
                            height: 30.h,
                            alignment: Alignment.center,
                            decoration: BoxDecoration(
                                color: Colors.blue[300],
                                borderRadius: BorderRadius.circular(
                                    ScreenUtil().setWidth(10))),
                            child: Text(
                              "游戏结算",
                              style: TextStyle(
                                  color: Colors.white,
                                  fontSize:
                                      MahjongSystemConst.bodyTextFontMiniSize,
                                  fontWeight: FontWeight.w600),
                            ))),
                  ],
                )),
          ));
    });
  }

  Container buildUserBillItem(
      {Color backgroundColor: Colors.green, MahjongUserInfo userInfo}) {
    return Container(
      height: bodyHeight / 9,
      decoration: BoxDecoration(
          color: backgroundColor, borderRadius: BorderRadius.circular(2.2)),
      margin: EdgeInsets.only(bottom: bodyHeight / 9 / 7),
      child: Row(
        children: [
          Expanded(
            flex: 3,
            child: Row(
              crossAxisAlignment: CrossAxisAlignment.center,
              mainAxisAlignment: MainAxisAlignment.start,
              children: [
                ClipRRect(
                    borderRadius: BorderRadius.circular(2.2),
                    child: userInfo.sysUser.portraitUrl == null
                        ? Image.asset(
                            "images/mahjong/user/header/common_header.png",
                            fit: BoxFit.cover,
                          )
                        : CachedNetworkImage(
                            fit: BoxFit.cover,
                            height: bodyHeight / 9,
                            width: bodyHeight / 9,
                            placeholder: (context, url) =>
                                CircularProgressIndicator(),
                            errorWidget: (context, url, error) =>
                                Icon(Icons.error),
                            imageUrl: DictionaryConst.filePath +
                                userInfo.sysUser.portraitUrl,
                          )),
                SizedBox(
                  width: ScreenUtil().setWidth(5),
                ),
                Column(
                  mainAxisAlignment: MainAxisAlignment.end,
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text(
                      "昵称:" + userInfo.sysUser.username,
                      style: TextStyle(
                          fontSize: MahjongSystemConst.bodyTextFontMiniSize),
                    ),
                    Text(
                      "ID:" + userInfo.sysUser.id.toString(),
                      style: TextStyle(
                          fontSize: MahjongSystemConst.bodyTextFontMiniSize),
                    )
                  ],
                )
              ],
            ),
          ),
          Expanded(
            flex: 3,
            child: Text(
              '胡牌次数：' + userInfo.winCount.toString(),
              style: TextStyle(
                  fontSize: MahjongSystemConst.bodyTextFontDefalutSize),
            ),
          ),
          Expanded(
              flex: 1,
              child: Text(
                userInfo.integral.toString() + '分',
                style: TextStyle(
                    color: userInfo.integral < 0 ? Colors.red : Colors.orange,
                    fontSize: MahjongSystemConst.bodyTextFontDefalutSize,
                    fontWeight: FontWeight.w500),
              )),
        ],
      ),
    );
  }
}
